﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;


using EntitySpaces.Interfaces;
using yncTech.BusinessObjects;

public partial class Admin_SAPFin_SAPFin001 : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["sd"] != null)
            {
                txtStartDate.Value = Session["sd"].ToString();
            }
            else
            {
                txtStartDate.Value = DateTime.Today.AddMonths(-1).ToString("yyyy-MM-dd");
            }

            if (Session["ed"] != null)
            {
                txtEndate.Value = Session["ed"].ToString();
            }
            else
            {
                txtEndate.Value = DateTime.Today.ToString("yyyy-MM-dd");
            }

            if (Session["txtFileName"] != null)
            {
                txtFileName.Text = Session["txtFileName"].ToString();
            }
            if (Session["drpCreator"] != null)
            {
                drpCreator.SelectedValue = Session["drpCreator"].ToString();
            }
        }
    }

    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        FinUploadLogCollection finUploadLogCollection = new FinUploadLogCollection();
        ArrayList args = new ArrayList();

        //args.Add(finUploadLogCollection.Query.CreatedBy == LoginUserInfo.CurrentUser.UserName);
        if (!string.IsNullOrEmpty(txtStartDate.Value))
        {
            args.Add(finUploadLogCollection.Query.CreateDttm >= txtStartDate.Value);
        }
        if (!string.IsNullOrEmpty(txtEndate.Value))
        {
            args.Add(finUploadLogCollection.Query.CreateDttm <= DateTime.Parse(txtEndate.Value).AddDays(1));
        }
        if (!string.IsNullOrEmpty(txtFileName.Text))
        {
            args.Add(finUploadLogCollection.Query.ExcelName.Like("%" + txtFileName.Text.Trim() + "%"));
        }
        if (!string.IsNullOrEmpty(drpCreator.SelectedValue))
        {
            args.Add(finUploadLogCollection.Query.CreatedBy == drpCreator.SelectedValue);
        }
        finUploadLogCollection.Query.Where(args.ToArray());
        finUploadLogCollection.Query.OrderBy(finUploadLogCollection.Query.SapUploadId.Descending);
        finUploadLogCollection.Query.es.Top = 100;
        DataTable tableSorce = finUploadLogCollection.Query.LoadDataTable();
        pageGrid.DataSource = tableSorce;
        pageGrid.CurrentPage = e.NewPageIndex;
        pageGrid.DataBind();
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["sd"] = txtStartDate.Value;
        Session["ed"] = txtEndate.Value;
        Session["drpCreator"] = drpCreator.SelectedValue;
        Session["txtFileName"] = txtFileName.Text.Trim();
    }

    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
        Session.Remove("sd");
        Session.Remove("ed");
        Session.Remove("txtFileName");
        Session.Remove("drpCreator");
    }

    #region  Delete_Command
    protected void Delete_Command(object sender, CommandEventArgs e)
    {
        decimal Id = decimal.Parse(e.CommandArgument.ToString());
        FinReceiptCollection finReceiptCollection = new FinReceiptCollection();
        finReceiptCollection.Query.Where(finReceiptCollection.Query.UploadId == Id, finReceiptCollection.Query.StorageStatus != "S1");
        finReceiptCollection.Query.Load();
        if (finReceiptCollection.Count > 0)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showMsg('此Excel上传的凭证有" + finReceiptCollection.Count.ToString() + "份已经入库，不能批量删除！')", true);
        }
        else
        {
            using (esTransactionScope tr = new esTransactionScope())
            {
                finReceiptCollection = new FinReceiptCollection();
                finReceiptCollection.Query.Where(finReceiptCollection.Query.UploadId == Id);
                finReceiptCollection.Query.Load();
                foreach (FinReceipt temp in finReceiptCollection)
                {
                    temp.IsDeleted = true;
                }
                finReceiptCollection.Save();

                FinUploadLog finUploadLog = new FinUploadLog();
                if (finUploadLog.LoadByPrimaryKey(Id))
                {
                    finUploadLog.IsDeleted = true;
                    finUploadLog.Save();

                }
                tr.Complete();
                ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showMsg('此Excel上传的凭证有已经成功批量删除！')", true);
            }
        }

    }
    #endregion

}